package mysql

import "common/models"

func (m Mysql) CommentCheckByUid(uid string) (b bool, ID int, err error) {
	var (
		count   int64
		comment *models.CommentInfo
	)
	b = false
	err = m.Db.Table("comment_info").Where("comment_id = ?", uid).Count(&count).Error
	if err != nil {
		return
	}
	if count > 0 {
		b = true
		m.Db.Table("comment_info").Where("comment_id = ?", uid).First(&comment)
		ID = int(comment.ID)
		return
	}
	return
}
func (m Mysql) CommentInfoAdd(comment *models.CommentInfo) (err error) {
	return m.Db.Table("comment_info").Create(&comment).Error
}

func (m Mysql) CommentInfoQueryByUid(uid string) (res *models.CommentInfo, err error) {
	err = m.Db.Table("comment_info").Where("comment_id = ?", uid).First(&res).Error
	return
}
func (m Mysql) CommentInfoQueryById(id string) (res *models.CommentInfo, err error) {
	err = m.Db.Table("comment_info").Where("id = ?", id).First(&res).Error
	return
}
func (m Mysql) CommentInfoQueryByAuthId(authId string, page, size int) (res []*models.CommentInfo, total int64, err error) {
	err = m.Db.Table("comment_info").Where("author_id = ?", authId).Offset((page - 1) * size).Limit(size).
		Find(&res).Count(&total).Error
	return
}
func (m Mysql) CommentInfoSaveById(comment *models.CommentInfo) (err error) {
	err = m.Db.Table("comment_info").Where("ID = ?", comment.CommentId).Save(&comment).Error
	return
}

func (m Mysql) CommentInfoDelById(ID string) (err error) {
	err = m.Db.Table("comment_info").Unscoped().Where("id = ?", ID).Delete(&models.CommentInfo{}).Error
	return
}

func (m Mysql) CommentInfoQueryByVideoIdLeveOne(videoId string, page, size int) (res []*models.CommentInfo, total int64, err error) {
	err = m.Db.Table("comment_info").Where("video_id = ? and parent_id = ? ", videoId, "|").
		Offset((page - 1) * size).Limit(size).Find(&res).Count(&total).Error
	return
}
